<template>
  <div class="cart_container">
    <div class="header">
      <div class="search">
        <div class="icon">
          <img src="../../assets/icon/common/search_333.png" alt="" />
        </div>
        <div class="bar">
          <input type="text" placeholder="搜索商品信息" @click="goSearch"/>
        </div>
      </div>
    </div>
    <div class="aside">
      <ul class="as_scroll">
        <li
          v-for="(ele, i) in nameList"
          :key="i"
          @click="tabList(i)"
          :class="index == i ? 'active' : ''"
        >
          {{ ele }}
        </li>
      </ul>
    </div>
    <div class="content">
      <div class="wrap">
        <div class="wr_item" ref="a" v-for="item in itemList" :key="item.id">
          <div class="title">
            <span class="line"></span>
            <span class="tit">{{ item.cate_name }}</span>
            <span class="line"></span>
          </div>
          <div class="category">
            <a
              v-for="ele in item.children"
              :key="ele.id"
              @click="goGoodsList(ele.id, ele.cate_name)"
            >
              <img :src="ele.pic" alt="" />
              <p>{{ ele.cate_name }}</p>
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "Category",
  data() {
    return {
      nameList: [],
      itemList: [],
      index: 0,
      timer: null,
      flag: true,
    };
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      let url = "/api/category";
      let pro = axios
        .get(url)
        .then((response) => {
          let { status, data } = response.data;
          if (status == 200) {
            let name = [];

            data.forEach((item) => {
              name.push(item.cate_name);

              this.itemList.push(item);
            });

            this.nameList = name;
          }
          console.log("数据获取成功", response.data);
        })
        .catch((err) => {
          console.log("获取数据失败");
        });
    },

    tabList(i) {
      this.index = i;
      if(i >= this.nameList.length - 2 - 1)
      {
        i = this.nameList.length - 2 - 1
      }
      let b = this.$refs.a[i].offsetTop - 48;
      this.flag = false;
      this.animate(window, b, () => {
        this.flag = true;
      });
    },
    scrollTxt() {
      window.addEventListener("scroll", () => {
        if (this.flag) {
          this.$refs.a.forEach((item, index) => {
            let scr = window.scrollY;
            if (scr >= item.offsetTop - 96) {
              this.index = index;
            } else if (scr == 820) {
              this.index = this.$refs.a.length - 1;
            }
          });
        }
      });
    },

    goGoodsList(id, title) {
      this.$router.push(`/goods_list?id=${id}&title=${title}`);
    },
    animate(obj, target, callback) {
      if (obj.timer) {
        clearInterval(obj.timer);
      }

      obj.timer = setInterval(() => {
        let step = (target - window.pageYOffset) / 10;
        step = step > 0 ? Math.ceil(step) : Math.floor(step);
        if (window.pageYOffset == target) {
          clearInterval(obj.timer);
          callback && callback();
        }

        window.scroll(0, window.pageYOffset + step);
      }, 15);
    },
    goSearch(){
      this.$router.push('/search')
    }
  },
  mounted() {
    this.scrollTxt();
  },
};
</script>
<style lang="scss" scoped>
.header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 48px;
  padding: 7.5px 13px 0;
  background-color: #fff;
  border-bottom: 1px solid #f5f5f5;
  .search {
    width: 100%;
    height: 33px;
    border-radius: 16.5px;
    background-color: #f5f5f5;
    display: flex;
    align-items: center;
    padding-left: 12px;
    .icon {
      width: 18px;
      height: 18px;
      margin-right: 10px;
      > img {
        width: 100%;
        height: 100%;
        vertical-align: top;
      }
    }
    .bar {width:80%;
      > input {
        width: 100%;
        display: block;
        outline: none;
        height: 25px;
        background-color: #f5f5f5;
        border: 0;
        font-size: 13px;
        color: #333;
        
      }
    }
  }
}
.aside::-webkit-scrollbar {
  display: none;
}
.aside {
  width: 90px;
  height: 100vh;
  position: fixed;
  top: 48px;
  left: 0;
  background-color: #f7f7f7;
  padding-bottom: 70px;
  overflow-y: auto;
  ul {
    li {
      width: 90px;
      height: 40px;
      text-align: center;
      line-height: 40px;
      font-size: 13px;
      color: #424242;
    }

    li.active {
      color: #fc4141;
      background-color: #fff;
      font-weight: 700;
      border-left: 2px solid #fc4141;
    }
  }
}
.content {
  background-color: #fff;
  padding-left: 90px;
  padding-top: 48px;
  .wrap {
    width: 100%;
    padding-bottom: 70px;
    .wr_item {
      padding-top: 15px;
      .title {
        height: 45px;
        display: flex;
        align-items: center;
        justify-content: center;
        .line {
          width: 50px;
          height: 1px;
          background-color: #999;
        }
        .tit {
          color: #333;
          font-size: 14px;
          font-weight: 700;
          padding: 0 15px;
        }
      }

      .category {
        width: 100%;
        padding: 0 12px;
        display: flex;
        flex-wrap: wrap;
        a {
          flex: 33.3%;
          margin-top: 15px;
          display: block;
          text-align: center;
          > img {
            width: 60px;
            height: 60px;
            border-radius: 50%;
          }
          p {
            width: 100%;
            // padding: 8px;
            color: #333;
            font-size: 12px;
            text-align: center;
          }
        }
      }
    }
  }
}
</style>